package com.lzb.newloan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.utils.CircularImage;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.Formatdou;
import com.lzb.newloan.utils.HttpUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.lzb.newloan.utils.BaseActivity;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 借款人详情
 */
public class BorrowDetailActivity extends BaseActivity implements
		OnClickListener {

	private TextView name,name2;
	private TextView sfz;
	private TextView money;
	private TextView day;
	private TextView zmxy;
	private String userId;
	private CircularImage img_icon;
	private String jkid,loan_id;

	private String Amt = "2000";// 金额
	private String MchntOrdId = "";// 商户订单号
	private String realname;
	private String idno, cardno;
	private TextView rzmobile;
	private TextView checknetdai;
	private TextView checkcourt;
	private TextView all_money;
	private TextView checksfz;
	private TextView financial_call_record;
	private TextView call_record;
	private TextView electricity_consum;
	private TextView title_right_txt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_go_invest);
		initView();
		initData();
	}

	private void initView() {
		SharedPreferences sp = getSharedPreferences("config", 0x0000);
		userId = sp.getString("userid", "");
		MchntOrdId = userId + System.currentTimeMillis();
		Intent intent = getIntent();
		jkid = intent.getStringExtra("jkid");
		findViewById(R.id.backpress).setOnClickListener(this);
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("借款人详情");
		title_right_txt = (TextView) findViewById(R.id.title_right_txt);
		title_right_txt.setVisibility(View.INVISIBLE);
//		title_txt_center.setText("立即添加");
//		title_right_txt.setOnClickListener(this);
		name = (TextView) findViewById(R.id.name);
		name2 = (TextView) findViewById(R.id.name2);
		realname = intent.getStringExtra("name");
		sfz = (TextView) findViewById(R.id.sfz);
		idno = intent.getStringExtra("idno");
		money = (TextView) findViewById(R.id.money);

		day = (TextView) findViewById(R.id.day);
		all_money = (TextView) findViewById(R.id.all_money);

		rzmobile = (TextView) findViewById(R.id.rzmobile);
		checknetdai = (TextView) findViewById(R.id.checknetdai);
		checkcourt = (TextView) findViewById(R.id.checkcourt);
		checksfz = (TextView) findViewById(R.id.checksfz);
		financial_call_record = (TextView) findViewById(R.id.financial_call_record);
		call_record = (TextView) findViewById(R.id.call_record);
		electricity_consum = (TextView) findViewById(R.id.electricity_consum);
		TextView credit_cards = (TextView) findViewById(R.id.credit_cards);
		TextView credit_cards_fee = (TextView) findViewById(R.id.credit_cards_fee);
		zmxy = (TextView) findViewById(R.id.zmxy);
		TextView problem = (TextView) findViewById(R.id.problem);
		problem.setOnClickListener(this);
		TextView fx = (TextView) findViewById(R.id.fx);
		fx.setOnClickListener(this);
		TextView jk_btn = (TextView) findViewById(R.id.jk_btn);
		jk_btn.setOnClickListener(this);
		img_icon = (CircularImage) findViewById(R.id.img_icon);

	}


	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			String result = msg.obj.toString();
			switch (msg.what) {

				case Config.CODE_URL_ERROR:
					Toast.makeText(BorrowDetailActivity.this, "url错误",
							Toast.LENGTH_SHORT).show();
					break;
				case Config.CODE_NET_ERROR:
					Toast.makeText(BorrowDetailActivity.this, "网络错误",
							Toast.LENGTH_SHORT).show();
					// if (time!=null) {
					// time.cancel();
					// }
					break;
				case Config.CODE_BORROWPERSONDETAIL:
					try {
						JSONObject response = new JSONObject(result);
						int error = response.getInt("error");
						System.out.println(response.toString());
//						if (error != 0) {
//							finish();
//						} else {
							JSONObject jkinfo = response
									.getJSONObject("jkinfo");
							String zm_score = jkinfo.getString("zmscore");
							if (zm_score.equals("null")||zm_score.equals("")) {
								zmxy.setText("暂无");
							}else {
								zmxy.setText(zm_score);
							}
							name.setText(jkinfo.getString("name"));
							name2.setText(jkinfo.getString("name"));
							sfz.setText(jkinfo.getString("idno"));
							money.setText(Formatdou.formatdou(jkinfo.getDouble("jk_money")));
							rzmobile.setText(jkinfo.getString("phone"));
							int jk_date = jkinfo.getInt("jk_date");
							if(jk_date==1){
								day.setText("15天");
							}else{
								day.setText("30天");
							}
							all_money.setText(Html.fromHtml("到期本息约："+"<font color=\"#FF532B\">"+jkinfo.getDouble("jk_rate")+ "元"+"</font>"));
							checknetdai.setText(jkinfo.getString("wdblack"));
							checkcourt.setText(jkinfo.getString("fyblack"));
							checksfz.setText(jkinfo.getString("sfzblack"));
							financial_call_record.setText(jkinfo.getString("jrphone"));
							call_record.setText(jkinfo.getString("tonghua"));
							electricity_consum.setText(jkinfo.getString("jd"));
							ImageLoader.getInstance().displayImage(response.getString("living_photo"),img_icon);

//						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;

				case Config.CODE_CANCLEBORROWPERSON:

					try {
						JSONObject response = new JSONObject(result);
						int error = response.getInt("error");
						if (error == 0) {
						}else {

						}

					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					break;
				default:
					break;
			}
		};
	};

	private void initData() {
		HttpUtils.doPostAsyn(Config.BORROWPERSONDETAIL_CORD + "&jkid="
				+ jkid + "&userid=" + userId, "",mHandler, Config.CODE_BORROWPERSONDETAIL);

	}





	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backpress:
			finish();

			break;
		case R.id.title_right_txt://立即添加
				break;
		case R.id.jk_btn:
            Intent intents = new Intent(this, InvestPersonPayActivity.class);
            intents.putExtra("jkid",jkid);
            startActivity(intents);
			break;
		case R.id.fx:
			Intent intent = new Intent(BorrowDetailActivity.this,
					ProblemActivity.class);
			intent.putExtra("title", "风险提示书");
			intent.putExtra("url", Config.FX);
			startActivity(intent);
			break;
		case R.id.problem:
			Intent intentp = new Intent(BorrowDetailActivity.this,
					ProblemActivity.class);
			intentp.putExtra("title", "借款协议");
			intentp.putExtra("url", Config.INVEST_JK);
			startActivity(intentp);
			break;
		default:
			break;
		}
	}

	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub

	}

}
